package com.jd.fruit.controller.admin.shop;

import java.io.IOException;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jd.fruit.common.annotation.Controller;
import com.jd.fruit.common.controller.BaseController;
import com.jd.fruit.common.util.DictUtil;
import com.jd.fruit.common.util.StringUtil;
import com.jd.fruit.model.admin.shop.Attribute;
import com.jd.fruit.model.admin.shop.Category;
import com.jd.fruit.model.admin.shop.CategoryAttribute;
import com.jfinal.kit.StrKit;

/**
 * 商品分类管理
 * 
 * @author lishunfeng
 * 
 */
@Controller("/admin/shop/category")
public class CategoryController extends BaseController {
	/**
	 * 添加商品分类
	 */
	public void add() {
		this.getRequest().setAttribute("oprtype", DictUtil.oprSave);
		List<Attribute> attributes=Attribute.dao.list();
		this.setAttr("attributes", attributes);
		render("category_add.html");
	}

	/**
	 *商品分类数据列表
	 */
	public void list() {
		//Category category = Category.dao;
		//List<Category> list = category.list();
		//this.getRequest().setAttribute("list", list);
		render("category_list.html");
	}

	/**
	 * 编辑商品分类
	 */
	public void detail() {
		String id = StringUtil.getDcodePara(getRequest(), "id");
		Category category = Category.dao.findById(id);
		String parentId = category.get("parent_id");
		if (StrKit.notBlank(parentId)) {
			String parentName = category.dao.findById(parentId).get("cat_name");
			getRequest().setAttribute("parentName", parentName);
		}
		//属性
		List<Attribute> attributes=Attribute.dao.list();
		//属性所有分类 及对应的信息
		List<CategoryAttribute>  cateAttrs= CategoryAttribute.dao.listByCat(id);
		this.setAttr("attributes", attributes);
		this.setAttr("cateAttrs", cateAttrs);
		this.setAttr("category", category);
		this.setAttr("oprtype", DictUtil.oprUpdate);
		render("category_add.html");// 这个是页面的命名规范
	}

	/**
	 * 删除商品分类
	 * 
	 * @throws IOException
	 */
	public void delete() throws IOException {
		String id = StringUtil.getDcodePara(getRequest(), "id");
		boolean isOk = Category.dao.deleteById(id);
		if (isOk) {// 删除成功
			this.renderText(DictUtil.success);
		} else { // 删除失败
			this.renderText(DictUtil.failed);
		}
	}

	/**
	 *保存商品分类
	 */
	public void save() {
		//读取json 数据
		String cat_name = this.getPara("catName");
		String oprtyp = this.getPara("oprtype");
		String updateid=this.getPara("id");
		String parentId=this.getPara("parentId");
		String sort_order=this.getPara("sortOrder");
		String attrs=this.getPara("attrs");
	
		//保存别名 如果存在父级把父级的别名加上自己的名称 否则保存自己的名称
		Category parent=null;
		String catId="";
		Category category =Category.dao;
		category.set("cat_name",cat_name); 
		category.set("sort_order",sort_order); 
		try {
			if(StrKit.notBlank(parentId)){
				String  aliasName=null;
			    parent=Category.dao.getParent(parentId);
				if(null!=parent){
					aliasName=parent.getStr("alias_name")+"-"+category.getStr("cat_name");
					category.set("alias_name",aliasName); 
				}else{
					aliasName=category.getStr("cat_name");
					category.set("alias_name", aliasName); 
				}
			}
			//获得保存时Id值
			if (oprtyp.equals(DictUtil.oprSave)) {
				catId=Category.dao.save(category);
			} else {
				catId=updateid;
				CategoryAttribute.dao.delAttrValuesRelationCat(updateid); 
				category.set("id", updateid);
				category.update();
			}
			CategoryAttribute.dao.save(attrs,catId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		JSONObject result=new JSONObject();
		result.put("code", DictUtil.success);
		this.renderJson(result);
	}

	/**
	 * 加载商品分类Jason数据
	 * 
	 * @throws IOException
	 */
	public void loadData() {
		String type = getPara("type");
		String result = "";
		JSONArray jsarray = null;
		if (!StrKit.isBlank(type) && type.equals(DictUtil.jsontree)) {
			String rolemap="";
			jsarray = Category.dao.loadTree(rolemap);
			if (jsarray != null) {
				result = jsarray.toJSONString();
			}
		} else {
			result = Category.dao.loadTreeGrid();
		}
		renderJson(result);
	}

}
